package com.czxy.service.impl;

import com.czxy.mapper.QuestionMapper;
import com.czxy.mapper.RecordMapper;
import com.czxy.pojo.TQuestion;
import com.czxy.pojo.TRecord;
import com.czxy.pojo.TUser;
import com.czxy.service.QuestionService;
import com.czxy.service.UserService;
import com.czxy.service.common.AbstractBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author likk
 * @create 2018/9/5
 */
@Service
@Transactional
public class QuestionServiceImpl extends AbstractBaseService<TQuestion> implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public List<TQuestion> findQuestionListByCustomsPassId(Integer customsPassId, Integer userId) {

        Example example = new Example(TQuestion.class);
        Example.Criteria criteria = example.createCriteria();

        example.orderBy("sort"); //排序

        // 查询条件
        criteria.andEqualTo("customsPassId", customsPassId); //关卡id
        criteria.andEqualTo("status", "1"); //启用状态

        List<TQuestion> list = questionMapper.selectByExample(example);

        // 查询当前用户当前关卡闯关记录
        // 查询条件
        TRecord record = new TRecord();
        record.setStudentId(userId);
        record.setCustomsPassId(customsPassId);

        TRecord tRecord = recordMapper.selectOne(record);

        // 设置哪个问题为当前正在答题的那个问题
        for (TQuestion question : list) {
            if (tRecord != null && question.getId().equals(record.getQuestionId())) {
                question.setAnswering(true);
            }
        }

        return list;
    }
}
